package com.sskj.wallet.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.TransferListBean;
import com.sskj.wallet.presenter.RecordTransferPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/06/27
 */
@Route(path = RConfig.WALLET_ACTIVITY_RECORD_TRANSFER)
public class RecordTransferActivity extends BaseActivity<RecordTransferPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    @Autowired
    String walletType;

    private BaseAdapter<TransferListBean> adapter;

    private int page = 1;
    private int size = 10;

    @Override
    public int getLayoutId() {
        return R.layout.wallet_activity_record_transfer;
    }

    @Override
    public RecordTransferPresenter getPresenter() {
        return new RecordTransferPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        tvTitle.setText(App.INSTANCE.getString(R.string.wallet_recordTransferActivity1));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintColor(Color.TRANSPARENT)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 2))
                .setFirstDraw(false));
        adapter = new BaseAdapter<TransferListBean>(R.layout.wallet_item_record_transfer, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, TransferListBean item) {
                holder.setText(R.id.tv_memo, item.getMemo())
                        .setText(R.id.tv_amount, item.getNum() + " " + item.getPname().toUpperCase())
                        .setText(R.id.tv_date, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000));
            }
        };
    }

    @Override
    public void initData() {
        mPresenter.getTransferRecord(true, page, size, walletType);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getTransferRecord(false, page, size, walletType);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getTransferRecord(false, page, size, walletType);
        });
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RecordTransferActivity.class);
        context.startActivity(intent);
    }

    public void getTransferRecordOK(List<TransferListBean> data) {
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
    }
}
